﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Modelling;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Modelling.Components.Actions;
using ITAP.ShprossesBuilder.Properties;
using ITAP.ShprossesBuilder.Editor;
using ITAP.ShprossesBuilder.Calculations;
using ITAP.Core.Modelling;
using ITAP.Core.Calculations;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationSetShprossesAnglesRelativeToAxes : IOperation
    {
        private ModelControl modelControl;
        #region IOperation Members

        public string Name
        {
            get { return "Задать угол между профилем и осями"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossOperation; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.ShprossSetAngleBetweenAxes.ToBitmap(); }
        }

        public void Execute(ModelControl modelControl)
        {
            this.modelControl = modelControl;
            ContextMenuStrip menuStrip = new ContextMenuStrip();

            EventHandler handler = new EventHandler(OnItemClick);
            ToolStripItem horizontal = new ToolStripMenuItem("Относительно горизонтальной оси", Resources.Horizontal.ToBitmap(), handler);
            horizontal.Tag = ComponentAlign.Horizontal;
            menuStrip.Items.Add(horizontal);

            ToolStripItem vertical = new ToolStripMenuItem("Относительно вертикальной оси", Resources.Vertical.ToBitmap(), handler);
            vertical.Tag = ComponentAlign.Vertical;
            menuStrip.Items.Add(vertical);
      
            menuStrip.Show(Cursor.Position);             


        }

        public bool CanUndo
        {
            get { return true; }
        }

        #endregion

        private void OnItemClick(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            ComponentAlign align = (ComponentAlign)item.Tag;

            SetShprossAngleForm angleForm = new SetShprossAngleForm();
            if (angleForm.ShowDialog() != DialogResult.OK)
                return;

            Angle angle = angleForm.Angle;
            bool copy = angleForm.Rotate;

            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
            {
                if (selectedComponent is Shpross)
                {
                    Shpross shpross;


                    if (!copy)
                        shpross = selectedComponent as Shpross;
                    else
                    {
                        shpross = selectedComponent.Copy() as Shpross;
                        shpross.SerialNumber = 0;
                    }
                    selectedComponent.Parent.AddInnerComponent(shpross);
                    ModelVector tangent = null;
                    if (shpross.SelectedPoint == ShprossPoints.Start)
                    {
                        tangent = shpross.Primitives[0].GetTangent(0);
                    }
                    else if (shpross.SelectedPoint == ShprossPoints.End)
                    {
                        tangent = shpross.Primitives[0].GetTangent(1).Rotate(new Angle(Math.PI));
                    }
                    else
                        return;

                    Angle angleBetweenShprossAndAxis;
                    if(align == ComponentAlign.Horizontal)
                        angleBetweenShprossAndAxis = ModelCalculations.GetAngleBetweenVectors(new ModelVector(1, 0), tangent, false);
                    else
                        angleBetweenShprossAndAxis = ModelCalculations.GetAngleBetweenVectors(new ModelVector(0, 1), tangent, false);

                    Angle angleToRotate = angleBetweenShprossAndAxis - angle;                    
                    shpross.Rotate(-angleToRotate.Value, shpross.SelectedPointPosition, shpross.Parent.BoundingBox);
                }                
            }

            this.modelControl.UpdateModel();
        }

    }
}
